{include file="public/layout" /}
<body>

<div class="layui-fluid " id="LAY-component-layer-list">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="head-oper">
                    <div class="fl">权限编辑</div>
                    <!--<div class="fr"> <a  class="layui-btn layui-btn-xs" href="admin.html" ><i class="layui-icon layui-icon-left"></i>返回</a></div>-->
                </div>
                <div class="layui-card-body layui-form house-form " wid100>
                    <div class="layui-form-item mb30">
                        <strong class="pl10">权限组基本信息</strong>
                        <hr class="layui-bg-gray2">
                    </div>
                    <div class="layui-form-item mb30">
                        <label class="layui-form-label"><b>*</b>组名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" id="name" value="{$info.name|default=''}" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item mb20">
                        <strong class="pl10">快捷勾选权限</strong>
                        <hr class="layui-bg-gray2">
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">参考权限组</label>
                        <div class="layui-input-inline">
                            <input type="radio"  name="tmp_role_id" lay-filter="tmp_role_id" value="-1" title="超级管理员" >
                            {foreach name="admin_role_list" item="role" key="k"}
                            <input type="radio" name="tmp_role_id" lay-filter="tmp_role_id" value="{$role.id}" title="{$role.name}" {eq name="$info.id" value="$role.id"}checked{/eq}>
                            {/foreach}
                        </div>
                    </div>
                    <div class="layui-form-item mb20">
                        <strong class="pl10">权限组权限设置</strong>
                        <hr class="layui-bg-gray2">
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">在线升级</label>
                        <div class="layui-input-inline w500">
                            <input type="checkbox" name="online_update" value="1"{eq name="$info.online_update" value="1"} checked="checked"{/eq} lay-skin="primary" title="允许操作" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">文档权限</label>
                        <div class="layui-input-inline w500">
                            <input type="checkbox" name="only_oneself" value="1"{eq name="$info.only_oneself" value="1"} checked="checked"{/eq} lay-skin="primary" title="只允许查看自己发布的文档" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">操作权限</label>
                        <div class="layui-input-inline w500">
                            <input type="checkbox" id="select_cud" {if condition="! empty($info.cud) && count($info.cud)>=3"} checked="checked"{/if} lay-filter="select_cud"  lay-skin="primary" title="完全控制" >
                            <input type="checkbox" name="cud[]" value="add" {if condition="! empty($info.cud) && in_array('add', $info.cud)"} checked="checked"{/if} lay-filter="cud" lay-skin="primary" title="添加信息">
                            <input type="checkbox" name="cud[]" value="edit" {if condition="! empty($info.cud) && in_array('edit', $info.cud)"} checked="checked"{/if} lay-filter="cud" lay-skin="primary" title="修改信息" >
                            <input type="checkbox" name="cud[]" value="del" {if condition="! empty($info.cud) && in_array('del', $info.cud)"} checked="checked"{/if} lay-filter="cud" lay-skin="primary" title="删除信息" >
                            <input type="checkbox" name="cud[]" value="changetableval" {if condition="! empty($info.cud) && in_array('changetableval', $info.cud)"} checked="checked"{/if} lay-filter="cud" lay-skin="primary" title="审核信息" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><b>*</b>功能权限</label>
                        <div class="layui-input-inline w500">
                            <input type="checkbox" id="select_all_permission" lay-filter="select_all_permission" lay-skin="primary" title="全部选择" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="edit-box-con2 fl" >
                        {foreach name="modules" item="vo"}
                            {if condition="1 == $vo['is_modules'] AND !empty($auth_rule_list[$vo.id])"}
                            {if condition="1000 == $vo['id']"}
                            {if condition="! empty($arctypes)"}
                            <fieldset class="layui-elem-field house-form-admin bgf6" >
                                <div class="layui-field-box">
                                    <label >{$vo.name}</label><br>
                                    {php}$first_arctype_id = '';{/php}
                                    <div class="fl">
                                    {foreach name="arctypes" item="arctype"}
                                        <span class="checkbox-bt" >
                                            {if condition="isset($arctype_array[$arctype['id']])"}
                                            {if condition="$first_arctype_id <> ''"}
                                            <em class="but off" data-id="{$arctype.id}"></em>
                                            {else /}
                                            <em class="but on" data-id="{$arctype.id}"></em>
                                            {php}$first_arctype_id = $arctype['id'];{/php}
                                            {/if}
                                            {/if}
                                            <input type="checkbox" lay-filter="arctype_cbox" class="arctype_cbox arctype_id_{$arctype.id}" name="permission[arctype][]" value="{$arctype.id}" {if condition="! empty($info.permission.arctype) && in_array($arctype.id, $info.permission.arctype)"} checked="checked"{/if} lay-skin="primary" title="{$arctype.typename}" >
                                        </span>
                                        {/foreach}
                                    </div>
                                    {foreach name="arctypes" item="arctype" key="k"}
                                    {if condition="isset($arctype_array[$arctype['id']])"}
                                    <div class="fl checkbox-box arctype_child"  id="arctype_child_{$arctype['id']}" {if condition="$first_arctype_id==$arctype['id']"} style="display: block;"{else /}style="display: none;"{/if}>
                                    {foreach $arctype_array[$arctype['id']] as $item}
                                        <div>
                                            <input type="checkbox" lay-filter="arctype_cbox" class="arctype_cbox arctype_id_{$item.id}" name="permission[arctype][]" value="{$item.id}" data-pid="{$item.parent_id}" {if condition="! empty($info.permission.arctype) && in_array($item.id, $info.permission.arctype)"} checked="checked"{/if} lay-skin="primary" title="{$item.typename}"  >
                                        </div>
                                        {if condition="isset($arctype_array[$item['id']])"}
                                        <div class="son"  id="arctype_child_{$item['id']}">
                                            &nbsp;&nbsp;|--&nbsp;
                                            {foreach $arctype_array[$item['id']] as $vo}
                                            <input type="checkbox" lay-filter="arctype_cbox" class="arctype_cbox" name="permission[arctype][]" value="{$vo.id}" data-pid="{$vo.parent_id}" data-tpid="{$item.parent_id}" {if condition="! empty($info.permission.arctype) && in_array($vo.id, $info.permission.arctype)"} checked="checked"{/if} lay-skin="primary" title="{$vo.typename}"  >
                                            {/foreach}
                                        </div>
                                        {/if}
                                    {/foreach}
                                    </div>
                                    {/if}
                                    {/foreach}
                                </div>
                            </fieldset>
                            {/if}
                            {else /}
                            {empty name="$vo.child"}
                            <fieldset class="layui-elem-field bgf6">
                                <div class="layui-field-box">
                                    <label>{$vo.name}</label><br>
                                    {foreach name="auth_rule_list[$vo.id]" item="rule"}
                                    <input type="checkbox"  name="permission[rules][]" value="{$rule.id}" {if condition="! empty($info.permission.rules) && in_array($rule.id, $info.permission.rules)"} checked="checked"{/if} lay-skin="primary" title="{$rule.name}" >
                                    {/foreach}
                                </div>
                            </fieldset>
                            {else /}
                            <fieldset class="layui-elem-field bgf6">
                                <div class="layui-field-box">
                                    <label>{$vo.name}</label><br>
                                    {foreach name="vo.child" item="vo2"}
                                    {if condition="1 == $vo2['is_modules'] AND !empty($auth_rule_list[$vo2.id])"}
                                    {foreach name="auth_rule_list[$vo2.id]" item="rule"}
                                    <input type="checkbox"  name="permission[rules][]" value="{$rule.id}" {if condition="! empty($info.permission.rules) && in_array($rule.id, $info.permission.rules)"} checked="checked"{/if} lay-skin="primary" title="{$rule.name}" >
                                    {/foreach}
                                    {/if}
                                    {/foreach}
                                </div>
                            </fieldset>
                            {/empty}
                            {/if}
                            {/if}
                        {/foreach}
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block  button-container layadmin-layer-demo">
                            <input type="hidden" name="id" value="{$info.id}">
                            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="formSubmit">确认提交</button>
                            <button class="layui-btn layui-btn-sm layui-btn-primary"  data-type="return_parent">返回</button>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var admin_role_list = {$admin_role_list|json_encode};
    $(function(){
        hasSelectAll();
    });
    function hasSelectAll(){
        var c = true;
        $('input[name^="permission"]').each(function(idx, ele){
            if(! $(ele).attr('checked')){
                c = false;
                return;
            }
        });
        if(c){
            $('#select_all_permission').attr('checked', 'checked');
        }else{
            $('#select_all_permission').removeAttr('checked');
        }

        var c = true;
        $('input[name^="cud"]').each(function(idx, ele){
            if(! $(ele).attr('checked')){
                c = false;
                return;
            }
        });
        if(c){
            $('#select_cud').attr('checked', 'checked');
        }else{
            $('#select_cud').removeAttr('checked');
        }
    }
    //快捷勾选权限
    function changeRole(value){
        if (-1 == value) {
            $('input[type="checkbox"]').attr("checked","checked");
            $('input[name="only_oneself"]').removeAttr('checked');
            return;
        }
        $('input[type="checkbox"]').removeAttr('checked');
        for(var i in admin_role_list){
            var item = admin_role_list[i];
            if(item.id == value){
//                if(item.language){
//                    item.language.map(function(row){
//                        $('input[name^="language"][value="'+row+'"]').attr('checked', 'checked');
//                    });
//                }
                if(item.online_update){
                    $('input[name="online_update"]').attr('checked', 'checked');
                };
                // if(item.editor_visual){
                //     $('#postForm input[name="editor_visual"]').attr('checked', 'checked');
                // };
                if(item.only_oneself){
                    $('input[name="only_oneself"]').attr('checked', 'checked');
                };
                if(item.cud){
                    item.cud.map(function(row){
                        $('input[name^="cud"][value="'+row+'"]').attr('checked', 'checked');
                    });
                }

                if(item.permission){
                    for(var p in item.permission){
                        if(p == 'plugins'){
                            if(item.permission[p]){
                                for(var pluginId in item.permission[p]){
                                    $('input[name="permission['+p+']['+pluginId+'][code]"][value="'+pluginId+'"]').attr('checked', 'checked');
                                    if(item.permission[p][pluginId].child){
                                        item.permission[p][pluginId].child.map(function(row){
                                            $('input[name="permission['+p+']['+pluginId+'][child][]"][value="'+row+'"]').attr('checked', 'checked');
                                        });
                                    }
                                }
                            }
                        }else{
                            $.each(item.permission[p], function(i,row) {
                                $('input[name="permission['+p+'][]"][value="'+row+'"]').attr('checked', 'checked');
                            });
//                            item.permission[p].map(function(row){
//                                $('input[name="permission['+p+'][]"][value="'+row+'"]').attr('checked', 'checked');
//                            });
                        }
                    }
                }
                hasSelectAll();
                break;
            }
        }
    }
    layui.config({
        base: '__SKIN__/' //静态资源所在路径
        ,version: '{$version}'
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form',  'upload'], function(){
        var $ = layui.$
            ,upload = layui.upload
            ,form = layui.form;

        var active = {
            return_parent:function () {     //返回上级
                var index=parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                parent.layer.close(index);		//关闭窗口
            }
        };
        $('#LAY-component-layer-list .layadmin-layer-demo .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] && active[type].call(this);
        });
        //权限选择显示效果
        $(".but").click(function() {
            var acid = $(this).data('id'), input = 'arctype_child_' + acid;
            $('.arctype_child').hide();
            if( $(this).attr('class').indexOf('off') == -1 ){
                $(this).removeClass('on').addClass('off');
            }else{
                $('.but').removeClass('on').addClass('off');
                $(this).removeClass('off').addClass('on');
                $('#'+input).show();
            }
        });
        //快捷勾选权限
        form.on('radio(tmp_role_id)',function (obj) {
            changeRole($(this).val());

            form.render('checkbox');
        });

        //选中所有操作权限
        form.on('checkbox(select_cud)',function (obj) {
            if($(this).attr('checked')){
                $('input[name^="cud"]').attr('checked',true);
            }else{
                $('input[name^="cud"]').attr('checked', false);
            }

            form.render('checkbox');
        });
        form.on('checkbox(cud)',function (obj) {
            hasSelectAll();
            form.render('checkbox');
        });
        //选中所有功能权限
        form.on('checkbox(select_all_permission)',function (obj) {
            if($(this).attr('checked')){
                $('input[name^="permission"]').attr('checked',true);
            }else{
                $('input[name^="permission"]').attr('checked', false);
            }

            form.render('checkbox');
        });
        //选中影响其他
        form.on('checkbox(arctype_cbox)',function (obj) {
            var acid = $(this).val(), input = 'arctype_child_' + acid;
            var pid = $(this).data('pid');
            var tpid = $(this).data('tpid');
            if($(this).attr('checked')){
                if (0 < $('input[data-pid="'+pid+'"]:checked').length) {
                    $('.arctype_id_'+pid).attr('checked', 'checked');
                }
                if (0 < $('#arctype_child_'+tpid).find('input[type="checkbox"]:checked').length) {
                    $('.arctype_id_'+tpid).attr('checked', 'checked');
                }
                $('#'+input).find('input[type="checkbox"]').attr('checked', 'checked');
            }else{
                if (1 > $('input[data-pid="'+pid+'"]:checked').length) {
                    $('.arctype_id_'+pid).removeAttr('checked');
                }
                if (1 > $('#arctype_child_'+tpid).find('input[type="checkbox"]:checked').length) {
                    $('.arctype_id_'+tpid).removeAttr('checked');
                }
                $('#'+input).find('input[type="checkbox"]').removeAttr('checked');
            }
            hasSelectAll();
            form.render('checkbox');
        });
        //监听提交
        form.on('submit(formSubmit)', function(data){
            if($('#name').val() == '' ){
                showErrorMsg('组名称不能为空！');
                $('input[name=name]').focus();
                return false;
            }
            var a = [];
            $('input[name^=permission]').each(function(i,o){
                if($(o).is(':checked')){
                    a.push($(o).val());
                }
            })
            if(a.length == 0){
                showErrorMsg('请具体分配权限！');
                return false;
            }
            var load = layer_loading();
            data.field._ajax = 1;
            $.ajax({
                type : 'post',
                url : "{:url('AuthRole/edit')}",
                data : data.field,
                dataType : 'json',
                success : function(res){
                    layer.close(load); //关闭loading
                    if(res.code == 1){
                        parent.window.location.href = res.url;
                    }else{
                        layer.msg(data.msg, {icon: 2,time: 2000});
                    }
                },
                error: function(e){
                    layer.close(load); //关闭loading
                    showErrorAlert();
                }
            });
            return false;
        });


    });
</script>
{include file="public/footer" /}